<template>
  <div class="infoConfig">
    <el-form ref="form" :model="form" :rules="rules">
      <div class="leftLine title">机构信息</div>
      <el-row style="padding: 0 20px;">
        <el-col :span="5" class="line40 pl20">机构</el-col>
        <el-col :span="7" class="line40">
          <span>{{ form.deptName }}</span>
        </el-col>
        <el-col :span="5" class="line40">业务编号</el-col>
        <el-col :span="7" class="line40">
          <el-form-item label="" prop="deptId" class="mg-b-0">
            <span>{{ form.deptId }}</span>
          </el-form-item>
        </el-col>
      </el-row>
      <div v-for="item of listOption" :key="item.key">
        <div class="leftLine title">{{ item.title }}</div>
        <div>
          <el-form-item v-for="child of item.child" :key="child.key" label="" :prop="child.key">
            <div v-if="['add','edit'].includes(pageType)">
              <div class="orderLabel">{{ child.title }}</div>
              <el-select v-model="child.value" size="small" class="numInput" @change="(val) => childChange(child, val)">
                <el-option label="有维权期" value="1" />
                <el-option label="无维权期" value="0" />
              </el-select>
              <span v-if="child.value == 1">
                <el-input-number v-model="form[child.key]" class="numInput" size="small" :controls="false" :min="1" />
                天
              </span>
            </div>
            <div v-else>
              <span class="mg-r-20">
                <span>{{ form.interactionRatio }}</span>&nbsp;：&nbsp;10
              </span>
            </div>
          </el-form-item>
        </div>
      </div>
      <div v-if="['edit','add'].includes(pageType)" class="btnList textCenter">
        <el-button size="small" @click="$emit('close')">取消</el-button>
        <el-button type="primary" size="small" @click="submit">保存配置</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
import { updateOrgGeneralConf } from '@/api/mercComConfig'
export default {
  props: {
    infoConfig: {}
  },
  data() {
    return {
      text: '',
      options: [],
      form: {},
      listOption: [
        {
          title: '商城',
          key: 'sc',
          child: [
            { title: '物流订单', key: 'logisticsDay', value: '0' },
            { title: '外卖订单', key: 'takeOutDay', value: '0' },
            { title: '自提订单', key: 'pickUpDay', value: '0' }
          ]
        },
        {
          title: '点餐',
          key: 'dc',
          child: [
            { title: '先付后吃订单', key: 'beforeEatDay', value: '0' },
            { title: '先吃后付订单', key: 'afterEatDay', value: '0' }
          ]
        },
        {
          title: '预定',
          key: 'yd',
          child: [
            { title: '全日房订单', key: 'allRoomDay', value: '0' },
            { title: '钟点房订单', key: 'hourRoomDay', value: '0' },
            { title: 'KTV订单', key: 'ktvDay', value: '0' }
          ]
        },
        {
          title: '预约',
          key: 'yy',
          child: [
            { title: '预约订单', key: 'subscribeDay', value: '0' }
          ]
        }
      ],
      rules: {
        orgDiscountConf: [{ validator: this.valid, trigger: 'change' }],
        agtConfigList: [{ validator: this.valid, trigger: 'change' }],
        mercConfigList: [{ validator: this.valid, trigger: 'change' }],
        deptName: [{ validator: this.valid, trigger: 'change' }],
        deptId: [{ validator: this.valid, trigger: 'change' }],
        // flowRatio: [{ validator: this.valid, trigger: 'change' }],
        interactionRatio: [{ validator: this.valid, trigger: 'change' }]
      }
    }
  },
  computed: {
    pageType() { return this.infoConfig.type }
  },
  mounted() {
    if (this.pageType === 'add') {
      this.form = {}
    } else {
      const baseData = JSON.parse(JSON.stringify(this.infoConfig.data))
      this.form = {
        ...baseData
      }
    }
    this.initPage()
    console.log('this.form', this.form)
  },
  methods: {
    initPage() {
      this.listOption = this.listOption.map(item => {
        return {
          ...item,
          child: item.child.map(child => {
            return {
              ...child,
              value: this.form[child.key] && this.form[child.key] != 0 ? '1' : '0'
            }
          })
        }
      })
    },
    childChange(item, value) {
      if (value == '0') {
        this.form[item.key] = '0'
      } else {
        this.form[item.key] = ''
      }
    },
    elOptionChange(e) {
      this.$set(this.form, 'deptId', e)
    },
    submit() {
      console.log('form', this.form)
      if (!this.form.deptName) {
        this.$message.error('请选择机构')
        return
      }
      this.$refs['form'].validate((valid) => {
        console.log('valid', valid)
        if (valid) {
          if (this.pageType === 'add') {
            updateOrgGeneralConf(this.form).then(res => {
              this.$message.success('操作成功！')
              this.$emit('sucClose')
            })
          } else {
            updateOrgGeneralConf(this.form).then(res => {
              this.$message.success('操作成功！')
              this.$emit('sucClose')
            })
          }
        } else {
          this.$message.error('表单信息填写有误，请重新检查！')
          return false
        }
      })
    },
    valid(rule, value, callback) {
      const { field } = rule
      let text = ''
      switch (field) {
        case 'orgDiscountConf': {
          // 租户流量余额折扣配置
          if (this.form.orgDiscountConf.length) {
            this.form.orgDiscountConf.forEach((item, index) => {
              if (!item.singlePay || !item.discount) {
                text += `配置${index + 1}信息不完整 `
              }
            })
          } else {
            text = '至少填写一条配置'
          }
          text ? callback(text) : callback()
          break
        }
        default:
          callback()
          break
      }
    }
  }

}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.infoConfig{
  padding: 30px;
  .orderLabel{
    display: inline-block;
    width: 100px;
    margin-left: 20px;
  }
  .line40{
    line-height: 40px;
  }
  .title{
    font-size: 15px;
    color: #000;
    margin: 30px 0;
  }
  .numInput{
    width: 100px;
    margin: 0 10px;
  }
}
</style>
